using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

using iSonar.iCarus.Interfaces;
using iSonar.iCarus.Gui.Controls;
using iSonar.iCarus.Gui.Dialogs;

namespace iSonar.iCarus.App {
  public partial class iCarusForm : Form, IApplication {
    public iCarusForm() {
      InitializeComponent();
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
      OpenFileDialog _dlg = new OpenFileDialog();
      _dlg.Filter = @"ASN.1 file|*.*|PKCS#12 file|*.*|All files|*.*||";
      if (DialogResult.OK == _dlg.ShowDialog()) {
        addAsn1View(_dlg.FileName);
      }
    }

    private void addAsn1View(string fileName) {
      try {
        using (Stream s = File.OpenRead(fileName)) {
          Asn1ViewCtrl _ctrl = new Asn1ViewCtrl(this);
          _ctrl.Dock = DockStyle.Fill;
          _ctrl.Font = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular,
                                System.Drawing.GraphicsUnit.Point, ((byte) (238)));
          _ctrl.Location = new Point(3, 3);
          _ctrl.Name = "Asn1ViewCtrl";
          _ctrl.Size = new Size(483, 252);
          _ctrl.TabIndex = 3;
          _ctrl.SetContent(s);
          addMainViewPanel(Path.GetFileName(fileName), _ctrl);
        }
      } catch (Exception ex) {
        new AppError(ex).ShowDialog(this);
      }
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
      this.Close();
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
        iCarusAbout _about = new iCarusAbout();
        _about.ShowDialog(this);
    }


    private void openWebBrowser(string url) {
      System.Diagnostics.Process _proc = new System.Diagnostics.Process();
      _proc.EnableRaisingEvents = false;
      _proc.StartInfo.FileName = url;
      _proc.Start();
    }

    private void wWWToolStripMenuItem_Click(object sender, EventArgs e) {
      openWebBrowser(@"http://codeplex.com/iCarus");
    }

    private void addMainViewPanel(string header, UserControl ctrl) {
      TabPage _page = new TabPage();
      _page.Controls.Add(ctrl);
      _page.Location = new System.Drawing.Point(4, 22);
      _page.Name = "";
      _page.Padding = new System.Windows.Forms.Padding(3);
      _page.Size = new System.Drawing.Size(489, 258);
      _page.TabIndex = 0;
      _page.Text = header;
      _page.UseVisualStyleBackColor = true;
      tabControl1.Controls.Add(_page);
      tabControl1.SelectTab(_page);
    }

    private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
      OptionsForm _options = new OptionsForm();
      _options.ShowDialog(this);
    }

    public void CloseCurrentMainView() {
      if (null != tabControl1.SelectedTab) {
        tabControl1.Controls.Remove(tabControl1.SelectedTab);
      }
    }
    public void CloseAllMainViews() {
      tabControl1.Controls.Clear();
    }

    public void ShowItem(IViewee obj) {
      try {
        throw new NotImplementedException();
      } catch (Exception ex) {
        new AppError(ex).ShowDialog(this);
      }
    }

    public void ShowItemDetails(IViewee obj) {
      try {
        byte[] _buff = obj.GetObject() as byte[];
        if (null == _buff) {
          return;
        }
        hexViewCtrl1.setBytes(_buff);
      } catch (Exception ex) {
        new AppError(ex).ShowDialog(this);
      }
    }

    private void closeViewToolStripMenuItem_Click(object sender, EventArgs e) {
      CloseCurrentMainView();
    }

    private void closeAllViewsToolStripMenuItem_Click(object sender, EventArgs e) {
      CloseAllMainViews();
    }

    private void iCarusForm_DragDrop(object sender, DragEventArgs e) {
      try {
        IDataObject dob = e.Data;
        string[] formats = dob.GetFormats();
        string[] fileDrop = dob.GetData(DataFormats.FileDrop) as string[];
        foreach (string s in fileDrop) {
          addAsn1View(s);
        }
      } catch (Exception ex) {
        new AppError(ex).ShowDialog(this);
      }
    }

    private void iCarusForm_DragEnter(object sender, DragEventArgs e) {
      e.Effect = DragDropEffects.Link;
    }
  }
}